package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        dishService.saveWithFlavor(dishDTO);
        //删除缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);

        log.info("接口111");
        return Result.success();
    }

    /**
     * 菜品分页查询  分页查询一般返回PageResult这种数据
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询{}",dishPageQueryDTO);
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 删除菜品
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    //@RequestParam动态解析字符串成集合，前端传过来字符串，1,2,3,4这种字符串
    public Result delectDish(@RequestParam List<Long> ids){
        dishService.delectDish(ids);

        //将所有菜品缓存数据给清除，以dish_开头的
        //获取批量删除的所有key
        extracted("dish_*");

        return Result.success();
    }


    /**
     * 通过id查询回显，口味及其相关信息
     */
    @GetMapping("/{id}")
    @ApiOperation("通过Id进行查询")
    public Result<DishVO> getById(@PathVariable Long id){
        DishVO dishVO=dishService.getByIdwithFlavor(id);
        return Result.success(dishVO);
    }


    /**
     * 修改，包括口味
     */
    @PutMapping
    @ApiOperation("通过id进行菜品修改")
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.updateWithflavor(dishDTO);
        //TODO 很少去修改，所有这里以简单的形势去写,不考虑分类
        //获取批量删除的所有key
        extracted("dish_*");

        return Result.success();
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 菜品的起售与停售
     *
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品的起售与停shou")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        dishService.startOrStop(status,id);
        extracted("dish_*");
        return Result.success();
    }

    private void extracted(String patter) {
        //获取批量删除的所有key
        Set keys = redisTemplate.keys(patter);
        redisTemplate.delete(keys);
    }
}
